<template>
  <div class="app-container">
    <!-- 添加或修改项目基础信息管理对话框 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="生产单号" prop="produceNo">
        <el-input v-model="queryParams.produceNo" placeholder="请输入生产单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工程名称" prop="gongchengName">
        <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="需求日期" prop="saleDate">
        <el-date-picker clearable v-model="queryParams.saleDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择需求日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="xinghao">
        <el-input v-model="queryParams.xinghao" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!--<el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExportDetail" >导出
        </el-button>-->
      </el-form-item>
    </el-form>

    <!--<el-col :span="1.5">
      <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple"
                 @click="piliangFenpeiGx" v-if="!projectNo" v-hasPermi="['develop:YiFenPeiList:piliangEditProcessBnt']">批量更改工艺工序</el-button>
    </el-col>-->
    <el-table border :data="columns" ref="dragTable" row-key="id" v-loading="loading" @selection-change="handleSelectionChange"  @sort-change="handleSortChange"
              :max-height="tableMaxHeight" show-summary :summary-method="getSummaries">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="生产单号" align="center" prop="produceNo" />
      <el-table-column sortable label="订单编号" align="center" prop="projectNo"  show-overflow-tooltip/>
      <el-table-column sortable label="客户名称" align="center" prop="customName" show-overflow-tooltip />
      <el-table-column sortable label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip />
      <<!--el-table-column sortable label="产品编码" align="center" prop="code" />-->
      <el-table-column sortable align="center"  label="产品编码" prop="code">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="showDetailGongxu(scope.row)" style="color: #00afff;cursor:pointer;">
              {{scope.row.code}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <!--<template slot-scope="scope">
        <span>{{ scope.row.code }}</span>
      </template>
    </el-table-column>-->
      <el-table-column sortable align="center" label="产品名称" prop="name" show-overflow-tooltip />
      <!-- <el-table-column sortable align="center" label="产品型号" prop="xinghao" />-->
      <el-table-column sortable align="center"  label="产品型号" prop="xinghao">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="showDetail(scope.row)" style="color: #00afff;cursor:pointer;">
              {{scope.row.xinghao}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <!--<el-table-column sortable align="center" label="订单数量" prop="orderCount" />
      <el-table-column sortable align="center" label="锁定数量" prop="lockCount" />-->
      <el-table-column align="center" label="自制数量" prop="count" />
      <!--<el-table-column align="center" label="巡检单" prop="xunjianFile" width="200">
        <template slot-scope="scope" v-if="scope.row.xunjianFile">
          <image-preview :src="scope.row.xunjianFile" height="60px" width="40px"></image-preview>
        </template>
      </el-table-column>-->
      <el-table-column label="下单日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求日期" align="center" prop="saleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="产品描述" prop="miaoshu" show-overflow-tooltip />
      <el-table-column align="center" label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <!--<el-button @click="choseGy(scope.row)" icon="el-icon-view" v-if="scope.row.status!=9"
                     size="mini" type="text">
            设定工序2
          </el-button>-->
          <el-button @click="lookGy(scope.row)" icon="el-icon-view"  v-if="scope.row.status!=9"
                     size="mini" type="text" v-hasPermi="['develop:YiFenPeiList:lookProcessBnt']">
            工序查看
          </el-button>
          <el-button @click="toOther(scope.row)" icon="el-icon-view"   v-if="scope.row.status!=9"size="mini" type="text">
            产品不自制
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <choseGx-form @ok="getList" ref="choseGxForm"></choseGx-form>
    <piliangFenpeiGxp-form @ok="getList" ref="piliangFenpeiGxpForm"></piliangFenpeiGxp-form>
    <chajie-form @ok="getList" ref="chaijieForm"></chajie-form>
    <fpTaskForm-list @ok="getList" ref="taskFpForm"></fpTaskForm-list>
    <xunjian-form @ok="getList" ref="xunjianForm"></xunjian-form>
    <goods-sale-good-gongxu-form @ok="getList" ref="GoodsSaleGoodGongxuForm"></goods-sale-good-gongxu-form>
    <toOther-form @ok="getList" ref="toOtherform"></toOther-form>
    <shengchanDoGoodsSale-form @ok="getList" ref="shengchanDoGoodsSaleForm"></shengchanDoGoodsSale-form>
    <shengchanDoGongxu-form @ok="getList" ref="shengchanDoGongxuForm"></shengchanDoGongxu-form>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList" />
  </div>
</template>

<script>
  import {
    addAction,
    deleteAction,
    downloadFile,
    getAction,
    getById,
    accAdd
  } from '@/api/manage'
  import ChoseGxForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChoseGxForm";
  import PiliangFenpeiGxpForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangFenpeiGxpForm";
  import ChajieForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChajieForm";
  import FpTaskFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/FpTaskFormList";
  import XunjianForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/XunjianForm";
  import GoodsSaleGoodGongxuForm from "@/views/develop/goodsSaledoGongxu/module/GoodsSaleGoodGongxuForm";
  import ToOtherForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ToOtherForm";
  import ShengchanDoGoodsSaleForm from '@/views/develop/goodsSale/module/ShengchanDoGoodsSaleForm.vue';
  import ShengchanDoGongxuForm from '@/views/develop/goodsSale/module/ShengchanDoGongxuForm.vue';

  export default {
    name: "ProductPaigongList",
    components: {
      ChoseGxForm,
      PiliangFenpeiGxpForm,
      ChajieForm,
      FpTaskFormList,
      XunjianForm,
      GoodsSaleGoodGongxuForm,
      ToOtherForm,
      ShengchanDoGoodsSaleForm,
      ShengchanDoGongxuForm,
    },
    props: {
      projectNo: {
        type: [String],
        required: true,
        default: ''
      },
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "设定产品工序",
        // 是否显示弹出层
        open: false,
        single: true,
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 表单参数
        form: {},
        proId: "",
        proStatus: "",
        columns: [],
        total: 0,
        ids: [],
        goodsIds: [],
        // 表单校验
        rules: {},
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          params: {},
          id: null,
          code: null,
          projectNo: null,
          gongchengName: null,
          goodsName: null,
          goodsCode: null,
          xinghao: null,
          name: null,
          //isdo: "1",//自制的
          //status:'2';
          issheding: "2",//已分配工艺工序
          remark: null,
          projectId: null,
          orderByColumn: "create_time",
          isAsc: "desc"
        },
        queryParams2: {
          pageNum: 1,
          pageSize: 10000,
          params: {},
          id: null,
        },
        url: {
          list: '/factoryProduceSaleAll/factoryProduceSaleAll/list',
          getById: '/projectBaseInfo/projectBaseInfo/',
          exportData: '/goodsSale/goodsSale/exportData',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    watch: {
      'projectCode': {
        handler(newVal, oldVal) {
          console.log("projectCode", newVal);
          if (newVal) {
            this.queryParams.projectNo = newVal;
            this.getList();
          }
        },
        deep: true,
        immediate: true
      },
    },
    created() {
      //this.handleQuery();
      this.getList();
      window.addEventListener('resize', this.getTableHeight);
    },
    methods: {
      showDetailGongxu(row){
        this.$refs.shengchanDoGongxuForm.show(row);
      },
      showDetail(row){
        this.$refs.shengchanDoGoodsSaleForm.show(row);
      },
      toOther(row){
        this.$refs.toOtherform.handleUpdate(row)
      },
      getSummaries(param) {
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = "合计";
            return;
          }
          const values = data.map((item) => Number(item[column.property]));
          // 只合计amount1和amount3，想合并哪些列就在这里指定
          if ( column.property === "count") {
            sums[index] = values.reduce((prev, curr) => {

              const value = Number(curr);
              if (!isNaN(value)) {
                return accAdd(prev , curr);
              } else {
                return prev;
              }
            }, 0);
          }
        });
        return sums;
      },
      lookGy(row){
        this.$refs.GoodsSaleGoodGongxuForm.xunjianShow(row);
      },
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.handleQuery();
      },
      xunjian(row) {
        this.$refs.xunjianForm.xunjianShow(row);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.goodsIds = selection.map(item => item.goodsId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      piliangFenpeiGx() {
        // 检查 this.goodsIds 中是否都一样，不一样进行提示
        if (this.goodsIds.length > 1) {
          const firstGoodsId = this.goodsIds[0];
          const allSame = this.goodsIds.every(goodsId => goodsId === firstGoodsId);
          if (!allSame) {
            this.$message.warning('选择的产品编码不一致，请重新选择');
          } else {
            this.$refs.piliangFenpeiGxpForm.piliangFenpeiGxpShow(this.ids, this.goodsIds);
          }
        } else {
          this.$refs.piliangFenpeiGxpForm.piliangFenpeiGxpShow(this.ids, this.goodsIds);
        }

      },
      taskFp(row) {
        this.$refs.taskFpForm.taskFpShow(row);
      },
      chaijie(row) {
        this.$refs.chaijieForm.chaijieGoods(row);
      },
      choseGy(row) {
        this.$refs.choseGxForm.choseGy(row);
      },

      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.params.salesmanName = ''
        this.queryParams.params.orderDate = ''
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      reset() {
        this.form = {
          id: null,
          remark: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },

      handleExportDetail(row) {
        downloadFile(this.url.exportData, this.queryParams);
      },

    }
  };
</script>
